﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using UTable.Objects.Handlers;
using UTable.Input.MultiTouch;
using UTable.Input;

namespace UTable.Objects.Controls
{
    public class UTabItem : TabItem, IControl
    {
        #region Constructor

        static UTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UTabItem), new FrameworkPropertyMetadata(typeof(UTabItem)));
        }

        public UTabItem()
        {
            InitializeIControlPart();
            DetectContactsHandler handler = new DetectContactsHandler();
            handler.NewContact += new ContactEventHandler(handler_NewContact);
            handler.NoContact += new ContactEventHandler(handler_NoContact);
            MultiTouchProcessor.Handlers.Add(handler);
            this.InputReceived += UTabItem_InputReceived;
        }

        void handler_NewContact(FingerEventArgs args)
        {
            if (!this.IsEnabled)
                return;
            this.IsSelected = true;
        }

        void handler_NoContact(FingerEventArgs args)
        {
            if (!this.IsEnabled)
                return;
            //TODO
        }

        void UTabItem_InputReceived(InputEventArgs args)
        {
            if (!this.IsEnabled)
                return;

            //args.Handled = true;
            //args.RouteDown = RouteEventDown;
        }

        //Fix: Will resulting in broken down
        //public bool RouteEventDown
        //{
        //    get { return (bool)GetValue(RouteEventDownProperty); }
        //    set { SetValue(RouteEventDownProperty, value); }
        //}

        //// Using a DependencyProperty as the backing store for RouteEventDown.  This enables animation, styling, binding, etc...
        //public static readonly DependencyProperty RouteEventDownProperty =
        //    DependencyProperty.Register("RouteEventDown", typeof(bool), typeof(UButton), new UIPropertyMetadata(true));

        #endregion

        #region IControl Implementation

        #region Private Fields

        private ControlBaseImpl _impl;

        #endregion

        #region Private Methods

        private void InitializeIControlPart()
        {
            _impl = new ControlBaseImpl(this);
            _impl.GotFingerCapture += new UTable.Input.MultiTouch.FingerEventHandler(_impl_GotFingerCapture);
            _impl.LostFingerCapture += new UTable.Input.MultiTouch.FingerEventHandler(_impl_LostFingerCapture);
            this.InputReceived += new UTable.Input.InputEventHandler(_impl_InputReceived);
        }

        void _impl_GotFingerCapture(object sender, UTable.Input.MultiTouch.FingerEventArgs args)
        {
            if (GotFingerCapture != null)
                GotFingerCapture(sender, args);
        }

        void _impl_LostFingerCapture(object sender, UTable.Input.MultiTouch.FingerEventArgs args)
        {
            if (LostFingerCapture != null)
                LostFingerCapture(sender, args);
        }

        void _impl_InputReceived(UTable.Input.InputEventArgs args)
        {
            if (args is UTable.Input.MultiTouch.MultiTouchEventArgs)
                MultiTouchProcessor.RaiseMultiTouchEvent(args as UTable.Input.MultiTouch.MultiTouchEventArgs);
        }

        #endregion

        #region Methods

        //TODO

        #endregion

        #region IControl Members

        public UTable.Objects.Controls.ControlInfo Info
        {
            get { return _impl.Info; }
        }

        public UTable.Objects.Handlers.MultiTouchProcessor MultiTouchProcessor
        {
            get { return _impl.MultiTouchProcessor; }
        }

        #endregion

        #region IVisualNode Members

        public String ID
        {
            get { return _impl.ID; }
        }

        public new double Width
        {
            get { return _impl.Width; }
            set { _impl.Width = value; }
        }

        public new double Height
        {
            get { return _impl.Height; }
            set { _impl.Height = value; }
        }

        public new bool IsHitTestVisible
        {
            get { return _impl.IsHitTestVisible; }
            set { _impl.IsHitTestVisible = value; }
        }

        #endregion

        #region IInputNode Members

        public void RaiseEvent(UTable.Input.InputEventArgs e)
        {
            if (InputReceived != null)
                InputReceived(e);
            OnInputReceived(e);
        }

        public virtual void OnInputReceived(UTable.Input.InputEventArgs args)
        {
            _impl.OnInputReceived(args);
        }

        public bool CaptureFinger(int fingerId)
        {
            return _impl.CaptureFinger(fingerId);
        }

        public bool UncaptureFinger(int fingerId)
        {
            return _impl.UncaptureFinger(fingerId);
        }

        public event UTable.Input.MultiTouch.FingerEventHandler GotFingerCapture;

        public event UTable.Input.MultiTouch.FingerEventHandler LostFingerCapture;

        public event UTable.Input.InputEventHandler InputReceived;

        #endregion

        #endregion
    }
}
